package lee.com.mqttlibrary.mqtt;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;

import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;

import lee.com.mqttlibrary.callback.MyMQTTCallback;

import static lee.com.mqttlibrary.model.Container.MQTT_CLIENT_ID;
import static lee.com.mqttlibrary.model.Container.MQTT_ISTOPIC;
import static lee.com.mqttlibrary.model.Container.MQTT_ISUNTOPIC;
import static lee.com.mqttlibrary.model.Container.MQTT_KEY;
import static lee.com.mqttlibrary.model.Container.MQTT_RECONNECT;
import static lee.com.mqttlibrary.model.Container.MQTT_TAG;
import static lee.com.mqttlibrary.model.Container.MQTT_TOPICS;

/**
 * CreateDate：18-10-31 on 上午11:16
 * Describe:
 * Coder: lee
 */
public class MQTTManager implements Presenter {

    private Context context;

    private MyServiceConnection connection;
    private MQTTService mqttService;
    private String topics;
    private String clientId;
    private boolean connect = false;

    private MQTTManager() {
    }

    public static MQTTManager getInstance() {
        return MQTTManagerHolder.instance;
    }

    private static class MQTTManagerHolder {
        private static final MQTTManager instance = new MQTTManager();
    }

    public boolean isConnect() {
        return connect;
    }

    public MQTTManager init(Context context, String clientId, String topics) {
        this.context = context;
        this.topics = topics;
        this.clientId = clientId;
        return getInstance();
    }

    public void reConnect(){
        Intent mqttIntent = new Intent(context, MQTTService.class);
        mqttIntent.putExtra(MQTT_RECONNECT, true);
        context.startService(mqttIntent);
    }

    @Override
    public MQTTManager bindMQTTService() {
        onBindService();
        return getInstance();
    }

    @Override
    public void unBindMQTTService() {
        onUnBindService();
    }

    @Override
    public MQTTManager subscribeTopic(String topic) {
        Intent mqttIntent = new Intent(context, MQTTService.class);
        mqttIntent.putExtra(MQTT_TAG, MQTT_ISTOPIC);
        mqttIntent.putExtra(MQTT_KEY, topic);
        context.startService(mqttIntent);
        return getInstance();
    }

    @Override
    public void unSubscribeTopic(String topic) {
        Intent mqttIntent = new Intent(context, MQTTService.class);
        mqttIntent.putExtra(MQTT_TAG, MQTT_ISUNTOPIC);
        mqttIntent.putExtra(MQTT_KEY, topic);
        context.startService(mqttIntent);
    }

    private void onBindService() {
        if (null == connection){
            connection = new MyServiceConnection();
            Intent i = new Intent(context, MQTTService.class);
            i.putExtra(MQTT_CLIENT_ID, clientId);
            i.putExtra(MQTT_TOPICS, topics);
            context.bindService(i, connection, context.BIND_AUTO_CREATE);
        }
    }

    private void onUnBindService() {
        if (null != connection){
            context.unbindService(connection);
            connect = false;
        }
    }

    private class MyServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d("MQTTManager", "onServiceConnected");
            mqttService = ((MQTTService.MqttBinder) service).getService();
            connect = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d("MQTTManager", "onServiceDisconnected");
            mqttService = null;
        }
    }

    public void sentMessage(String topic, String msg) {
         mqttService.sentMessage(topic, msg.getBytes());
    }

    public void sentMessage(String topic, byte[] msg) {
        mqttService.sentMessage(topic, msg);
    }

    public void setCallback(MyMQTTCallback mqttMessageCallback) {
        mqttService.setMqttMessageCallback(mqttMessageCallback);
    }

}
